package com.example.farmer.Adapter.Product;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.farmer.Entity.ChatMessage;
import com.example.farmer.R;

import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ChatViewHolder> {

    private List<ChatMessage> chatMessages;

    public ChatAdapter(List<ChatMessage> chatMessages) {
        this.chatMessages = chatMessages;
    }

    @NonNull
    @Override
    public ChatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item, parent, false);
        return new ChatViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ChatViewHolder holder, int position) {
        ChatMessage chatMessage = chatMessages.get(position);
        holder.messageTextView.setText(chatMessage.getMessageContent());
        // Set other properties like timestamp, sender, etc., if needed
    }

    @Override
    public int getItemCount() {
        return chatMessages.size();
    }

    // 向适配器添加新的消息
    public void addMessage(ChatMessage newMessage) {
        // 将新消息添加到列表末尾
        chatMessages.add(newMessage);
        // 通知适配器添加了新的消息
        notifyItemInserted(chatMessages.size() - 1);
    }

    static class ChatViewHolder extends RecyclerView.ViewHolder {
        TextView messageTextView;
        ImageView touxiang;

        ChatViewHolder(View itemView) {
            super(itemView);
            messageTextView = itemView.findViewById(R.id.message_text_view);
            touxiang = itemView.findViewById(R.id.touxiang);
        }
    }
}